package com.gxa.retail_admin.controller;

import com.gxa.common.vo.JsonResult;
import com.gxa.modules.pojo.R_Menu;
import com.gxa.modules.pojo.R_User;
import com.gxa.modules.service.LoginService;
import com.gxa.modules.service.UserService;
import com.gxa.modules.shiro.MyRealm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: wangyifan
 * @Date: 2021/12/29 15:06
 * @Description: $end$
 * @Version: V1.0
 */
@Controller
@Api(value = "登录模块",tags = "登录模块(王议帆)")
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private MyRealm myRealm;
    @Autowired
    private LoginService loginService;

    @ApiOperation(value = "登录接口")
    @PostMapping(value = "/login")
    @ResponseBody
    public JsonResult login(@RequestBody R_User user){
        JsonResult jsonResult = loginService.returnMenu(user.getUsername());
        //得到主体
        Subject subject = SecurityUtils.getSubject();
            if(!subject.isAuthenticated()){
                //获取Token : 给前端传
                UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getUser_password());
                //登录
                subject.login(token);
            }

        return jsonResult;
    }

}
